package reflect;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;

public class Test2 {
    public static void main(String[] args) throws Exception {
        File file= new File(Test2.class.getResource(".").toURI());
        System.out.println(file);

        File[] data = file.listFiles(f->f.getName().endsWith(".class"));
        for(File sub :data){
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.lastIndexOf("."));
            //String className = fileName.replace(".class","");
            String packName = Test2.class.getPackage().getName();
            className = packName+"."+className;
            Class cls = Class.forName(className);
            Object obj = cls.newInstance();
            System.out.println(obj);
            Method[] methods = cls.getDeclaredMethods();
            for(Method method : methods){
                if (method.getParameterCount()==0 && method.getModifiers()== Modifier.PUBLIC){
                    System.out.println("调用方法为"+method.getName());
                    method.invoke(obj);
                }
            }
        }
    }
}
